﻿@{
    Layout = "_LayoutAdmin";
}

@model IReadOnlyList<Moonglade.Model.PostMetaData>

@section head{
    <link href="~/css/datatables.min.css" rel="stylesheet" />
    <script src="~/js/datatables.min.js"></script>
}

@section scripts{
    <script>
        $(function () {
            $(".btn-delete").click(function () {
                var cfm = confirm("Delete Confirmation?");
                if (cfm) {
                    deletePost($(this).data("postid"));
                }
            });

            $("#posts-table").DataTable({
                "order": [[1, "desc"]],
                "lengthMenu": [[10, 15, 20, 25, 50, -1], [10, 15, 20, 25, 50, "All"]],
                "pageLength": 10
            });
        });

        function deletePost(postid) {
            $("#span-processing-" + postid).show();
            ajaxPostWithCSRFToken("manage/delete", { postid: postid }, function (data) {
                $("#tr-" + data).hide();
            });
        }
    </script>
}

<partial name="_PostManageHeader" />

@if (Model.Any())
{
    @Html.AntiForgeryToken()

    <table id="posts-table" class="table table-bordered">
        <thead>
            <tr>
                <th>Title</th>
                <th class="table-item-w20">Created</th>
                <th class="table-item-w20">Options</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var post in Model.OrderByDescending(p => p.CreateOnUtc))
            {
                <tr id="tr-@post.Id">
                    <td>
                        <a asp-controller="Post" asp-action="Slug"
                           asp-route-year="@post.PubDateUtc.Value.Year"
                           asp-route-month="@post.PubDateUtc.Value.Month"
                           asp-route-day="@post.PubDateUtc.Value.Day"
                           asp-route-slug="@post.Slug" target="_blank">
                            @post.Title
                        </a>
                    </td>
                    <td data-sort="@post.CreateOnUtc.ToString("yyyyMMddHHmmss")">
                        @post.CreateOnUtc
                    </td>
                    <td>
                        <a class="btn btn-primary btn-sm btn-edit" asp-controller="Post" asp-action="Edit" asp-route-id="@post.Id">
                            Edit
                        </a>
                        <a class="btn btn-danger btn-sm btn-delete" data-postid="@post.Id" href="javascript:;">
                            Delete
                        </a>
                        <span id="span-processing-@post.Id" style="display: none">...</span>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}
else
{
    <partial name="_NoPosts" />
}